package com.hzsai.kafka;

import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.kafka.support.SendResult;
import org.springframework.util.concurrent.ListenableFuture;

/**
 * @author: hzsai
 * @date : 2020/3/25
 * usage:
 **/
public class KafkaSender {

    private final KafkaTemplate<String, String> kafkaTemplate;

    public KafkaSender(KafkaTemplate<String, String> kafkaTemplate) {
        this.kafkaTemplate = kafkaTemplate;
    }

    /**
     * send message
     *
     * @param topic
     * @param message
     */
    public void send(String topic, String message) {
        this.kafkaTemplate.send(topic, message);
    }

    public ListenableFuture<?> sendAndResult(String topic, String message) {
        ListenableFuture<SendResult<String, String>>future =
                this.kafkaTemplate.send(topic, message);
        return future;
    }
}

